import { reactive } from 'vue'

export function useUser(){
  // 响应式用户数据
  const userInfo = reactive({
    id: '001',
    name: '张爷爷',
    avatar: '/static/default-avatar.svg',
    age: 68,
    level: 5,
    healthIndex: 88,
    continuousSignIn: 7
  })
  
  // 响应式天气数据
  const weatherInfo = reactive({
    temp: '24°C',
    desc: '晴朗',
    suitable: '宜外出'
  })
  
  // 响应式任务数据
  const taskInfo = reactive({
    completed: 3,
    total: 5,
    todayTasks: [
      { id: 1, name: '早晨散步', done: true },
      { id: 2, name: '按时服药', done: true },
      { id: 3, name: '午休30分钟', done: true },
      { id: 4, name: '下午社区活动', done: false },
      { id: 5, name: '晚饭后遛弯', done: false }
    ]
  })
  
  // 加载用户数据（模拟异步）
  const loadUserData = async () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        // 模拟从服务器获取数据
        console.log('用户数据加载完成')
        resolve(true)
      }, 300)
    })
  }
  
  // 更新用户信息
  const updateUserInfo = (data) => {
    Object.assign(userInfo, data)
  }
  
  // 完成任务
  const completeTask = (taskId) => {
    const task = taskInfo.todayTasks.find(t => t.id === taskId)
    if(task && !task.done){
      task.done = true
      taskInfo.completed += 1
    }
  }
  
  return { 
    userInfo, 
    weatherInfo, 
    taskInfo, 
    loadUserData,
    updateUserInfo,
    completeTask
  }
}

